//多光源支持
Shader "91make.top/SSLocalStudy"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "black" {}
		_Color("Color",Color) = (1,1,1,1)
		_Offset("Offset",Range(0,1)) = 0.5
		_Scale("Scale",Range(0,1)) = 0.5
        _MainColor("MainColor",Color) = (1,1,1,1)
        _SpecPower("SpecPower",Range(1,128)) = 0.5
        _SpecColor("SpecColor",Color) = (1,1,1,1)

        [Toggle]_SSS("是否次表面散射",int) = 1 //Toggle 开关复选框
        _DistoreFactor("法线扰动系数",Range(0,1)) = 0.5
        _BackPower("背光集中度",Range(1,4)) = 1
        _BackStrength("背光强度",Range(1,4)) = 0.5
        _BackAmbient("背光环境光",Range(0,4)) =0
        _ThicknessMap("厚度",2d) = "white" {}
        _ThicknessFactor("厚度系数",Range(0,1)) = 0.5
        [Toggle]_ThicknessInvert("是否反转厚度",int) = 1 //Toggle 开关复选框


	}
	SubShader
	{
		Tags
        { 
            "RenderType"="Opaque"
        }
		LOD 100
		Pass
		{
			Name "Unlit"
            Tags
            {
                "LightMode"="UniversalForward"
            }
		}
        HLSLPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
            half3 normalOS:NORMAL;
        };
        struct v2f
        {
            float2 uv : TEXCOORD0;
            float4 vertex : SV_POSITION;
            half3 normalWS:TEXCOORD1;
            half3 posWS:TEXCOORD2;


        };

        sampler2D _MainTex;
        float4 _MainTex_ST;
        half4 _MainColor;
        float4 _Color;
        float _Offset;
        half _SpecPower;
        half4 _SpecColor;
        half _DistoreFactor;
        half _BackPower;
        half _BackStrength;
        half _BackAmbient;
        // sampler2D _ThicknessMap;
        half _ThicknessFactor;
        half _ThicknessInvert;
        Texture2D(_ThicknessMap):SMAPLER(sampler_ThicknessMap);//采样器
        half _SSS;


        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = TransformObjectToHClip(v.vertex.xyz);
            o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            o.normalWS = TransformObjectToWorldNormal(v.normalOS);
            o.posWS = TransformObjectToWorld(v.vertex);
            return o;
        }

        half3 LightingSSS(half3 L,half3 N,half3 V,half thickness,half3 lightColor)
        {
            half3 H =L+N*_DistoreFactor;
            // half backIntensity = dot(V,-H);
            half backIntensity = pow(saturate(dot(V,-H)),_BackPower) * _BackStrength + _BackAmbient;
            // half thickness = SAMPLE_TEXTURE2D(_ThicknessMap,sampler_ThicknessMap,i.uv).r;
            backIntensity *= 1 - thickness;//传入厚度图，玉的bunny兔子
            return backIntensity * _MainColor * lightColor;
            // return pow(saturate(dot(V,-H)),_BackPower) * _BackStrength;

        }


        half4 frag (v2f i) : SV_Target
        {
        	half4 col = tex2D(_MainTex, i.uv);

            //正面光照(PBR中直接使用unity计算结果)
            half3 N = i.normalWS;
            half3 L = GetMainLight().direction;

            // half3 V = _WorldSpaceCameraPos.xyz - i.posWS;// 观察向量
            half3 V = normalize(_WorldSpaceCameraPos.xyz - i.posWS);// 观察向量
            half3 lv =  normalize(L + V);// 半角向量


            half nl = saturate(dot(N,L)); // 法线和光照的点积
           //漫反射
            half3 diff = mainLight.color * _MainColor * nl;
            col += half4(diff,1);
            //高光
            half nh = saturate(dot(N,lv));// 法线和半角的点积
            half3 spec = mainLight.color * _SpecColor * pow(nh,_SpecPower);
            col += half4(spec,1);


            //背面光照
            if(_SSS)
            {
                //直接光
            // L 到L+N做插值
            // half3 H =L+N*_DistoreFactor;
            // // half backIntensity = dot(V,-H);
            // half backIntensity = pow(saturate(dot(V,-H)),_BackPower) * _BackStrength + _BackAmbient;
            // half thickness = SAMPLE_TEXTURE2D(_ThicknessMap,sampler_ThicknessMap,i.uv).r;
            // backIntensity *= 1 - thickness;//传入厚度图，玉的bunny兔子
            // col += half4(backIntensity * _MainColor * mainLight.color,1);
            // return pow(saturate(dot(V,-H)),_BackPower) * _BackStrength;
                half thickness = SAMPLE_TEXTURE2D(_ThicknessMap,sampler_ThicknessMap,i.uv);
                 if(_ThicknessInvert){ //hlsl会编译成step语句
                    thickness = 1 - thickness;
                }
                thickness = lerp(1,thickness,_ThicknessFactor);
              
                return thickness;//调试

                col += half4(LightingSSS(
                    mainLight.direction,
                    N,
                    V,
                    thickness,
                    mainLight.color
                    )
                    ,1);

                //间接光(灯光颜色，衰减，方向和直接光照不同)
                for(int k = 0;k<GetAdditionalLightsCount();k++)
                {
                    Light light = GetAdditionalLightsCount(k,i.posWS);
                    col += half4(LightingSSS(
                        light.direction,
                        N,
                        V,
                        thickness,
                        light.color * light.distanceAttenuation
                        )
                       ,1);
                    
                }
            }


            return col;
        }
        ENDHLSL
    
    
    }
}